<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { ElMessage, type FormInstance, ElCard, ElButton, ElTag } from 'element-plus'
import { User, Lock, Key, Coffee, Clock, Calendar, View } from '@element-plus/icons-vue'
import type { Course } from '@/types/course.ts'; // 引入课程类型接口
import { useAuthStore } from "@/stores/auth.ts"
import { useRouter } from "vue-router"
import axios from "@/utils/request.ts"
//时间库
import { format } from 'date-fns';


const courses = ref<Course[]>([])
const router = useRouter()


//页码
const pageNum = ref(1)
//页码大小
const pageSize = ref(9);
//总量
const total = ref(0);
//搜索条件
const search = ref("")
const isStop = ref(false)
const isUploading = ref(false)

//定义时间类型
const formatDate = (dateString: string | Date | undefined) => {
  if (!dateString) return '无';
  return format(new Date(dateString), 'yyyy-MM-dd HH:mm');
};

const getAllCourse = () => {
  axios.get("/course/selectAllCourse",{params:{pageNum:pageNum.value,search:search.value,isStop:isStop.value}}).then(
      response => {
        courses.value = response.data.courses.list
        total.value = response.data.courses.total
      },
      error => {
        ElMessage.error('获取课程列表失败')
        console.error(error)
      }
  )
}

// 分页变化处理
const handlePageChange = (newPage:number) => {
  pageNum.value = newPage;
  getAllCourse();
};
onMounted(() => {
  getAllCourse()
})

// 查看课程详情
const viewCourseDetail = (courseId: number) => {
  router.push(`/resourse/${courseId}`)
}
</script>

<template>
  <div class="course-container">
    <div class="page-header">
      <h2 class="page-title">课程列表</h2>
      <div class="search-controls">
        <div class="search-input-wrapper">
          <el-input
              clearable
              v-model="search"
              placeholder="请输入课程名"
              class="search-input"
              size="large"
          />
        </div>
        <div class="filter-controls">
          <div class="filter-item">
            <span class="filter-label">未开始或已结束课程</span>
            <el-switch v-model="isStop" class="stop-switch"/>
          </div>
          <el-button
              @click="getAllCourse"
              type="primary"
              class="search-btn"
              size="large"
          >
            搜索
          </el-button>
        </div>
      </div>
    </div>

    <div class="course-filters" style="margin-bottom: 20px;">
      <ElTag v-if="courses.length === 0" type="info">
        暂无课程数据
      </ElTag>
    </div>

    <div class="course-cards">
      <ElCard
          v-for="course in courses"
          :key="course.id"
          class="course-card"
          shadow="hover"
      >
        <template #header>
          <div class="card-header">
            <h3 class="course-title">{{ course.courseName }}</h3>
            <ElTag
                :type="course.stop ? 'danger' : 'success'"
                :disable-transitions="false"
                class="status-tag"
            >
              {{ course.stop ? '已下架' : '课程进行中' }}
            </ElTag>
          </div>
        </template>

        <div class="course-info">
          <div class="info-item">
            <span class="info-label">课程介绍：</span>
            <p class="course-desc">{{ course.courseDesc || '暂无介绍' }}</p>
          </div>

          <div class="info-item">
            <span class="info-label">开放时间：</span>
            <span class="time-range">
              {{ formatDate(course.startTime) }} 至 {{ formatDate(course.endTime) }}
            </span>
          </div>

          <div class="info-item">
            <span class="info-label">开放时长：</span>
            <span class="duration">{{ course.openDuration }} 天</span>
          </div>
        </div>

        <div class="card-actions">
          <ElButton
              type="primary"
              :icon="View"
              @click="viewCourseDetail(course.id)"
              :disabled="!course.enable || course.stop"
              class="detail-btn"
          >
            课程资源
          </ElButton>
        </div>
      </ElCard>
    </div>

    <div class="pagination-container">
      <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="pageNum"
          @current-change="handlePageChange"
          :disabled="isUploading || total === 0"
          class="course-pagination"
      />
    </div>
  </div>
</template>

<style scoped>
.course-container {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e6e8eb;
}

.search-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end;
}

.search-input-wrapper {
  flex: 1;
  min-width: 300px;
}

.search-input {
  width: 30%;
}

.filter-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-label {
  font-size: 14px;
  color: #606266;
  white-space: nowrap;
}

.stop-switch {
  margin-left: 8px;
}

.search-btn {
  min-width: 100px;
}

.course-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 24px;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

.course-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  transition: all 0.3s ease;
  border: 1px solid #e6e8eb;
  min-height: 320px;
}

.course-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 16px 20px 0;
}

.course-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  flex: 1;
  margin-right: 12px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.status-tag {
  flex-shrink: 0;
  margin-top: 2px;
}

.course-info {
  flex: 1;
  padding: 16px 20px;
}

.info-item {
  margin-bottom: 16px;
  display: flex;
  align-items: flex-start;
}

.info-label {
  color: #606266;
  font-weight: 500;
  width: 80px;
  flex-shrink: 0;
  font-size: 14px;
  line-height: 1.5;
}

.course-desc {
  margin: 0;
  flex: 1;
  color: #303133;
  line-height: 1.6;
  font-size: 14px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.time-range {
  flex: 1;
  font-size: 14px;
  color: #303133;
  line-height: 1.5;
}

.duration {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
}

.card-actions {
  display: flex;
  justify-content: flex-end;
  padding: 16px 20px;
  border-top: 1px solid #f0f0f0;
}

.detail-btn {
  min-width: 100px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 24px;
}

.course-pagination {
  margin-top: 0;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .course-cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .course-container {
    padding: 16px;
  }

  .search-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .search-input-wrapper {
    min-width: auto;
  }

  .filter-controls {
    justify-content: space-between;
  }

  .course-cards {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .course-title {
    margin-right: 0;
  }
}
</style>
